//
//  MOView.m
//  UIViewAnimationTest
//
//  Created by mynSoo on 2017/1/8.
//  Copyright © 2017年 mynSoo. All rights reserved.
//

#import "HMLisenAudioLoadingView.h"
#import <AVFoundation/AVFoundation.h>
#import "HMListenAnyTimeResModel.h"

@interface HMLisenAudioLoadingView()

@property (nonatomic, assign) NSInteger animatCount;
@property (nonatomic, strong) NSArray *progressViewArr;
@end

@implementation HMLisenAudioLoadingView

-(void)setModelArr:(NSArray *)modelArr
{
    self.alpha = 0;
    self.animatCount = 0;
    CGFloat corner = 30;
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(corner, corner)].CGPath;
    self.layer.mask = shapeLayer;
    
    _modelArr = modelArr;
    NSMutableArray * arrMF = [NSMutableArray array];
    float total = 0;
    for(NSInteger i = 0; i < modelArr.count; i++)
    {
        HMListenAnyTimeResModel *model = modelArr[i];
        
        AVAsset *asset = [AVAsset assetWithURL:[NSURL URLWithString:model.audio_file_url]];
        
        total += CMTimeGetSeconds(asset.duration);
        [arrMF addObject:asset];
    }
    
    NSMutableArray * arrM = [NSMutableArray array];
    
    
    AVAsset *asset = arrMF[0];
    
    float durantion = CMTimeGetSeconds(asset.duration) / total;
    
    UIView *view = [[UIView alloc]init];
    
    [self addSubview:view];
    [arrM addObject:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left);
        make.centerY.offset(0);
        make.height.equalTo(self);
        make.width.equalTo(self).multipliedBy(durantion);
    }];
    view.backgroundColor = [UIColor colorWithHex:0xff962f];
    
    
    for(NSInteger i = 1; i < arrMF.count; i++)
    {
        AVAsset *asset = arrMF[i];
        
        float durantion = CMTimeGetSeconds(asset.duration) / total;
        
        UIView *view = [[UIView alloc]init];
        UIView *lastView = self.subviews.lastObject;
        [self addSubview:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(lastView.mas_right).offset(3);
            make.centerY.offset(0);
            make.height.equalTo(self);
            make.width.equalTo(self).multipliedBy(durantion);
        }];
        
        // current += self.bounds.size.width * durantion;
        
        
        view.backgroundColor = [UIColor colorWithHex:0xff962f];
        
        [arrM addObject:view];
    
    }
    self.progressViewArr = arrM;

}

-(void)animateCompletion
{
    
    self.animatCount ++;
    
    if(self.progressViewArr.count == self.animatCount)
    {
        
        if(self.finishBlock)
        {
            self.finishBlock(self);
        }
    }
}

- (void)animateProgress:(NSArray *)arr
{
    self.alpha = 1;
    
    for(NSInteger i = 0; i < arr.count; i++)
    {
        UIView *view = arr[i];
        
        view.transform = CGAffineTransformMakeTranslation([UIScreen mainScreen].bounds.size.width, 0);
        
        [UIView animateWithDuration:3.0 / arr.count delay:i * 3.0 / arr.count options:UIViewAnimationOptionTransitionNone animations:^{
            view.transform = CGAffineTransformIdentity;
        } completion:^(BOOL finished){
            [self animateCompletion];
            
        }];
    }

}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if(self)
    {
        
    }
    return self;
}

- (void)animate:(NSArray *)durationArray
{
    self.alpha = 0;
    
    self.animatCount = 0;
    
    CGFloat corner = 30;
    
    CAShapeLayer *shapeLayer = [CAShapeLayer layer];
    
    shapeLayer.path = [UIBezierPath bezierPathWithRoundedRect:self.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft | UIRectCornerTopRight | UIRectCornerBottomRight cornerRadii:CGSizeMake(corner, corner)].CGPath;
    
    self.layer.mask = shapeLayer;
    
    NSMutableArray *arrM = [NSMutableArray array];
    
    for (NSInteger i = 0; i < durationArray.count; i++)
    {
        
        NSNumber *duration = durationArray[i];
        
        UIView *view = [[UIView alloc]init];
        UIView *lastView = self.subviews.lastObject;
        [self addSubview:view];
        if(i == 0)
        {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(self);
                make.centerY.offset(0);
                make.height.equalTo(self);
                make.width.equalTo(self).multipliedBy(duration.doubleValue);
            }];
        }
        else
        {
            [view mas_makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(lastView.mas_right).offset(3);
                make.centerY.offset(0);
                make.height.equalTo(self);
                make.width.equalTo(self).multipliedBy(duration.doubleValue);
            }];
        }
    
        view.backgroundColor = [UIColor colorWithHex:0xff962f];
        
        [arrM addObject:view];
        
    }
    self.progressViewArr = arrM;
    
    [self animateProgress:arrM.copy];

}

@end
